import {
  asyncSetInterval,
  getSearchString
} from '../../lib/zeroReportUtils.js';

const pages = {
  reprotResult:
    'https://etax.shenzhen.chinatax.gov.cn/BsfwtWeb/apps/views/sb/cxdy/sbcx.html',
  sbbcx: 'https://etax.shenzhen.chinatax.gov.cn/yhs-web/sbbcx/view/sbList.html',
  tabelPage: 'https://etax.shenzhen.chinatax.gov.cn/yhs-web/api/sbcx/getSbbmx'
};

const sbbcx = async (tableDetailList) => {
  console.log('sbbcx');
  asyncSetInterval(() => {
    let trs = $('#tbody > tr');
    return Array.from(trs);
  }, 1000).then(async (trs) => {
    for (let i = 0; i < trs.length; i++) {
      await asyncSetInterval(() => {
        let onclick = $(trs[i]).find('td:eq(-1) > a').attr('onclick');
        if (onclick) {
          eval(onclick);
        }
        return true;
      }, 1000);
    }
    setTimeout(() => {
      window.location.href = 'https://etax.shenzhen.chinatax.gov.cn/BsfwtWeb/apps/views/sb/cxdy/sbcx.html?zeroReportEnd=1';
    }, 1000);
  });
};

const tabelPage = async () => {
  asyncSetInterval(() => {
    let tit = $('head > title').text();
    let tableContent;
    switch (tit) {
      case '增值税纳税申报表':
        tableContent = {
          declareType: '小规模增值税季报',
          title: tit,
          html: $('#printView').prop('outerHTML'),
          css:
            'table{position:relative;}table.border{border-left:1px solid #000;border-top:1px solid #000;}tbody{display:table-row-group;vertical-align:middle;border-color:inherit;}tr{display:table-row;vertical-align:inherit;border-color:inherit;}table.border th,table.border td{border-right:1px solid #000;border-bottom:1px solid #000;padding:1px;word-break:break-all;}'
        };
        window.postMessage(
          {
            event: 'asyncTableDetail',
            data: tableContent
          },
          '*'
        );
        break;
      case '增值税小规模纳税人-- 附列资料':
        tableContent = {
          declareType: '小规模增值税季报',
          title: tit,
          html: $('#printView').prop('outerHTML'),
          css:
            "table{position:relative;}table{font-family:'微软雅黑';border-collapse:collapse;border-spacing:0;width:90%;margin:0 auto;font-size:12px;}table.border{border-left:1px solid #000;border-top:1px solid #000;}table.border th,table.border td{border-right:1px solid #000;border-bottom:1px solid #000;padding:1px;word-break:break-all;}"
        };
        window.postMessage(
          {
            event: 'asyncTableDetail',
            data: tableContent
          },
          '*'
        );
        break;
      case '增值税减免税申报明细表':
        tableContent = {
          declareType: '小规模增值税季报',
          title: tit,
          html: $('#printView').prop('outerHTML'),
          css:
            "table{position:relative;}table{font-family:'微软雅黑';border-collapse:collapse;border-spacing:0;width:90%;margin:0 auto;font-size:12px;}table.border{border-left:1px solid #000;border-top:1px solid #000;}table.border th,table.border td{border-right:1px solid #000;border-bottom:1px solid #000;padding:1px;word-break:break-all;}"
        };
        window.postMessage(
          {
            event: 'asyncTableDetail',
            data: tableContent
          },
          '*'
        );
        break;
      case '附加税':
        tableContent = {
          declareType: '小规模增值税季报',
          title: tit,
          html:
            $('body > table:eq(0)').prop('outerHTML')
            + $('body > table:eq(1)').prop('outerHTML'),
          css:
            "table{position:relative;}table{font-family:'微软雅黑';border-collapse:collapse;border-spacing:0;width:90%;margin:0 auto;font-size:12px;}table.border{border-left:1px solid #000;border-top:1px solid #000;}table.border th,table.border td{border-right:1px solid #000;border-bottom:1px solid #000;padding:1px;word-break:break-all;}"
        };
        window.postMessage(
          {
            event: 'asyncTableDetail',
            data: tableContent
          },
          '*'
        );
        break;
      case '企业所得税月（季）度预缴纳税申报表（A类）':
        tableContent = {
          declareType: '企业所得税A类季报',
          title: tit,
          html: $('body > div:eq(0)').prop('outerHTML'),
          css:
            "table{position:relative;}table{font-family:'微软雅黑';border-collapse:collapse;border-spacing:0;width:90%;margin:0 auto;font-size:12px;}table.border{border-left:1px solid #000;border-top:1px solid #000;}table.border th,table.border td{border-right:1px solid #000;border-bottom:1px solid #000;padding:1px;word-break:break-all;}"
        };
        window.postMessage(
          {
            event: 'asyncTableDetail',
            data: tableContent
          },
          '*'
        );
        break;
      case 'A201020 资产加速折旧、摊销(扣除)优惠明细表':
        tableContent = {
          declareType: '企业所得税A类季报',
          title: tit,
          html: $('body > div:eq(0)').prop('outerHTML'),
          css:
            "table{position:relative;}table{font-family:'微软雅黑';border-collapse:collapse;border-spacing:0;width:90%;margin:0 auto;font-size:12px;}table.border{border-left:1px solid #000;border-top:1px solid #000;}table.border th,table.border td{border-right:1px solid #000;border-bottom:1px solid #000;padding:1px;word-break:break-all;}"
        };
        window.postMessage(
          {
            event: 'asyncTableDetail',
            data: tableContent
          },
          '*'
        );
        break;
      case 'A201010免税收入、减计收入、所得减免等优惠明细表':
        tableContent = {
          declareType: '企业所得税A类季报',
          title: tit,
          html: $('body > form').prop('outerHTML'),
          css:
            "table{position:relative;}table{font-family:'微软雅黑';border-collapse:collapse;border-spacing:0;width:90%;margin:0 auto;font-size:12px;}table.border{border-left:1px solid #000;border-top:1px solid #000;}table.border th,table.border td{border-right:1px solid #000;border-bottom:1px solid #000;padding:1px;word-break:break-all;}"
        };
        window.postMessage(
          {
            event: 'asyncTableDetail',
            data: tableContent
          },
          '*'
        );
        break;
      case 'A201030减免所得税优惠明细表':
        tableContent = {
          declareType: '企业所得税A类季报',
          title: tit,
          html:
            $('body > div:nth-child(1) > table:nth-child(1)').prop(
              'outerHTML'
            ) + $('#tabList').prop('outerHTML'),
          css:
            "table{position:relative;}table{font-family:'微软雅黑';border-collapse:collapse;border-spacing:0;width:90%;margin:0 auto;font-size:12px;}.bbbt{font-size:13pt;}table.border{border-left:1px solid #000;border-top:1px solid #000;}table.border th,table.border td{border-right:1px solid #000;border-bottom:1px solid #000;padding:1px;word-break:break-all;}"
        };
        window.postMessage(
          {
            event: 'asyncTableDetail',
            data: tableContent
          },
          '*'
        );
        break;
      default:
        break;
    }
    setTimeout(() => {
      window.close();
    }, 500);
  }, 1000);
};

const reprotResult = async (tableDetailList) => {
  console.log('tableDetailList', tableDetailList);
  let zeroReportEnd = getSearchString('zeroReportEnd');
  if (zeroReportEnd) {
    asyncSetInterval(async () => {
      let $els = $('#mini-grid-table-bodysbqkGrid > tbody > .mini-grid-row');
      let xgm = Array.from($els).find((item) =>
        $(item).text().includes('《增值税纳税申报表（小规模纳税人适用）》'));
      let qs = Array.from($els).find((item) =>
        $(item)
          .text()
          .includes(
            '《A200000中华人民共和国企业所得税月（季）度预缴纳税申报表（A类，2018年版）》'
          ));

      let hadXgmTable = tableDetailList.find(
        (item) => item.declareType === '小规模增值税季报'
      );
      let hadQsTable = tableDetailList.find(
        (item) => item.declareType === '企业所得税A类季报'
      );

      if (xgm && !hadXgmTable) {
        $(xgm).find('td:eq(-1) > a:eq(0)')[0].click();
        await asyncSetInterval(() => {
          window.close();
        }, 1000);
      } else if (qs && !hadQsTable) {
        $(qs).find('td:eq(-1) > a:eq(0)')[0].click();
        setTimeout(() => {
          window.close();
        }, 1000);
      } else {
        window.postMessage(
          {
            event: 'asyncTableDetailRequest',
            data: ''
          },
          '*'
        );
      }
      return true;
    }, 2000);
  }
};

export function asyncTable({ tableDetailList, isStart }) {
  let href = window.location.href;
  if (href.includes(pages.reprotResult)) {
    reprotResult(tableDetailList);
  }
  if (href.includes(pages.sbbcx)) {
    sbbcx(tableDetailList);
  }
  if (href.includes(pages.tabelPage)) {
    tabelPage();
  }
}
